package com.sh4.controller.xgfs;

import java.util.HashMap;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.sh4.entity.XgfsOrder;
import com.sh4.service.XgfsOrderService;

import net.sf.json.JSONObject;

@RestController
@RequestMapping(value = "xgfs/order")
public class OrderController {
	
	@Autowired
	XgfsOrderService xgfsOrderService;

	@RequestMapping(value = "/list" , method = RequestMethod.POST)
	public Map<String , Object> list(@RequestParam int pageNo , @RequestParam int pageSize , @RequestBody JSONObject reqBody){
		
		Map<String, Object> result = new HashMap<String,Object>();
		int status = 200;
		String msg = "提交成功";
		try {
			
			result.put("dataList", xgfsOrderService.list(pageSize, pageNo, reqBody));
			result.put("total", xgfsOrderService.countXgfsOrder(reqBody));
		} catch (Exception e) {
			status = 300;
			msg = "内部错误";
		}
		result.put("status", status); 
		result.put("msg", msg);
		
		return result;
	}
	
	@RequestMapping(value = "/" , method = RequestMethod.POST)
	public Map<String, Object> save(@RequestBody JSONObject xo) {
		
		Map<String, Object> result = new HashMap<String,Object>();
		int status = 200;
		String msg = "提交成功";
		try {
			
			xgfsOrderService.save(xo);
			result.put("data", xo);
		} catch (Exception e) {
			status = 300;
			msg = "内部错误";
		}
		result.put("status", status); 
		result.put("msg", msg);
		
		return result;
		
	}
	
	@RequestMapping(value = "/complete" , method = RequestMethod.POST)
	public Map<String, Object> orderComplete(@RequestBody JSONObject xo) {
		
		Map<String, Object> result = new HashMap<String,Object>();
		int status = 200;
		String msg = "提交成功";
		try {
			
			xgfsOrderService.uptadeOrderComplete(xo, "xgfs11001");
			result.put("data", xo);
		} catch (Exception e) {
			status = 300;
			msg = "内部错误";
		}
		result.put("status", status); 
		result.put("msg", msg);
		
		return result;
		
	}
	
	
	@RequestMapping(value = "/comment" , method = RequestMethod.POST)
	public Map<String, Object> comment(@RequestBody JSONObject xo) {
		
		Map<String, Object> result = new HashMap<String,Object>();
		int status = 200;
		String msg = "提交成功";
		try {
			
			if(xo.containsKey("speed") && xo.containsKey("quality") && xo.containsKey("comment") && xo.containsKey("id")) {
				xgfsOrderService.commentXgfsOrder(xo);
			}else {
				msg = "参数不正确!";
				status = 300;
			}
			
		} catch (Exception e) {
			status = 300;
			msg = "内部错误";
		}
		result.put("status", status); 
		result.put("msg", msg);
		
		return result;
		
	}
	
}
